//
//  CheckMapViewController.m
//  dangdang
//
//  Created by air on 16/2/24.
//  Copyright © 2016年 luweijie. All rights reserved.
//

#import "CheckMapViewController.h"
#import <AMapNaviKit/MAMapKit.h>
#import <AMapNaviKit/AMapNaviKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import "NavPointAnnotation.h"
#import "OriginAnnotationView.h"
#import "DestinationAnnotationView.h"

@interface CheckMapViewController ()<MAMapViewDelegate>
{
    MAMapView * _mapView;
    AMapLocationManager * _mapLocationManager;
}

@property (nonatomic, strong) AMapNaviPoint* startPoint;
@property (nonatomic, strong) AMapNaviPoint* endPoint;

@end

@implementation CheckMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"导航" style:UIBarButtonItemStylePlain target:self action:@selector(routeCal)];
    
    self.title = @"查看地图";
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
//    [self initNaviPoints];
    
    [self configMapView];
    
//    [self initAnnotations];
}

- (void)routeCal
{
    NSString *urlString = [NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&backScheme=iostest&slat=%f&slon=%f&sname=abc&sid=B001&dlat=%f&dlon=%f&dname=def&did=B002&dev=0&m=3&t=0", _startPoint.latitude, _startPoint.longitude, _endPoint.latitude, _endPoint.longitude];
    if ([self canOpenURL:[NSURL URLWithString:urlString]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }else{
        NSString *urlString = [NSString stringWithFormat:@"http://itunes.apple.com/cn/app//id461703208?mt=8"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }
}

- (BOOL)canOpenURL:(NSURL*)url{
    if ([url.scheme isEqualToString:@"iosamap"]) {
        return YES;
    }else{
        return NO;
    }
}

- (void)configMapView
{
    _mapLocationManager = [[AMapLocationManager alloc] init];
    
    [_mapLocationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    
    [_mapLocationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
            if (error)
            {
                NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
                
                if (error.code == AMapLocationErrorLocateFailed)
                {
                    return;
                }
            }
            
            //        NSLog(@"location:%@", location);
            if (regeocode)
            {
                _startPoint = [AMapNaviPoint locationWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
                
                _endPoint = [AMapNaviPoint locationWithLatitude:[_lat floatValue] longitude:[_lng floatValue]];
                
                [self initAnnotations];
            }
        }];
//    _endPoint = [AMapNaviPoint locationWithLatitude:[_lat floatValue] longitude:[_lng floatValue]];
//    [self initAnnotations];
}

#pragma mark - Construct and Inits

//- (void)initNaviPoints
//{
//    _startPoint = [AMapNaviPoint locationWithLatitude:23.132347 longitude:113.322571];
//    _endPoint   = [AMapNaviPoint locationWithLatitude:23.126180 longitude:113.282794];
//}

- (void)initAnnotations
{
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    [_mapView setDelegate:self];
    
    NavPointAnnotation *beginAnnotation = [[NavPointAnnotation alloc] init];
    [beginAnnotation setCoordinate:CLLocationCoordinate2DMake(_startPoint.latitude, _startPoint.longitude)];
    beginAnnotation.title = [NSString stringWithFormat:@"已发单: 99"];
    beginAnnotation.subtitle = [NSString stringWithFormat:@"已派送: 99"];
    beginAnnotation.navPointType = NavPointAnnotationStart;
    
    [_mapView setCenterCoordinate:beginAnnotation.coordinate];
    [_mapView selectAnnotation:beginAnnotation animated:YES];
    [_mapView addAnnotation:beginAnnotation];
    
    NavPointAnnotation *endAnnotation = [[NavPointAnnotation alloc] init];
    [endAnnotation setCoordinate:CLLocationCoordinate2DMake(_endPoint.latitude, _endPoint.longitude)];
    endAnnotation.title = @"刘德华";
    endAnnotation.navPointType = NavPointAnnotationEnd;
    
    [_mapView setCenterCoordinate:endAnnotation.coordinate];
    [_mapView selectAnnotation:endAnnotation animated:YES];
    [_mapView addAnnotation:endAnnotation];
    
    [self.view addSubview:_mapView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - MAMapView Delegate

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[NavPointAnnotation class]])
    {
        static NSString *annotationIdentifier = @"annotationIdentifier";
        
//        MAPinAnnotationView *pointAnnotationView = (MAPinAnnotationView*)[_mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
        
//        pointAnnotationView.animatesDrop   = NO;
//        pointAnnotationView.canShowCallout = YES;
//        pointAnnotationView.draggable      = NO;
        
        NavPointAnnotation *navAnnotation = (NavPointAnnotation *)annotation;
        
        if (navAnnotation.navPointType == NavPointAnnotationStart)
        {
            OriginAnnotationView *pointAnnotationView = (OriginAnnotationView*)[_mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
            if (pointAnnotationView == nil)
            {
                pointAnnotationView = [[OriginAnnotationView alloc] initWithAnnotation:annotation
                                                                       reuseIdentifier:annotationIdentifier];
            }
            pointAnnotationView.image = [UIImage imageNamed:@"hk_address.png"];
            //设置中心点偏移，使得标注底部中间点成为经纬度对应点
            pointAnnotationView.centerOffset = CGPointMake(0, -18);
            
            return pointAnnotationView;
        }
        else if (navAnnotation.navPointType == NavPointAnnotationEnd)
        {
            DestinationAnnotationView *pointAnnotationView = (DestinationAnnotationView*)[_mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
            if (pointAnnotationView == nil)
            {
                pointAnnotationView = [[DestinationAnnotationView alloc] initWithAnnotation:annotation
                                                                       reuseIdentifier:annotationIdentifier];
            }
//            UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(100, 0, 50, 150)];
//            [button addTarget:self action:@selector(ringUp) forControlEvents:UIControlEventTouchUpInside];
//            [pointAnnotationView addSubview:button];
            
//            pointAnnotationView.method = @selector(ringUp);
            
            pointAnnotationView.image = [UIImage imageNamed:@"hk_address2.png"];
            //设置中心点偏移，使得标注底部中间点成为经纬度对应点
            pointAnnotationView.centerOffset = CGPointMake(0, -18);
            
            return pointAnnotationView;
        }
    }
    
    return nil;
}

- (void)ringUp{
    NSLog(@"111111");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
